package com.hongyun.tms.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.hongyun.tms.common.PageResponseDto;
import com.hongyun.tms.common.constants.DefaultCreateByConstant;
import com.hongyun.tms.common.dto.prodconsume.ProdConsumeQueryDto;
import com.hongyun.tms.common.dto.prodconsume.ProdConsumeSaveReqDto;
import com.hongyun.tms.common.enums.BizCode;
import com.hongyun.tms.common.utils.DateUtils;
import com.hongyun.tms.common.utils.MapperingUtils;
import com.hongyun.tms.common.vo.ProdConsumeVO;
import com.hongyun.tms.entity.ProdConsume;
import com.hongyun.tms.exception.BusinessRuntimeException;
import com.hongyun.tms.mapper.ProdConsumeMapper;
import com.hongyun.tms.service.IProdConsumeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 作业计划 服务实现类
 * </p>
 *
 * @author liPeng
 * @since 2024-06-12
 */
@Service
public class ProdConsumeServiceImpl extends ServiceImpl<ProdConsumeMapper, ProdConsume> implements IProdConsumeService {

    @Override
    public PageResponseDto<ProdConsumeVO> treeList(ProdConsumeQueryDto query) {
        // 创建分页对象
        Page<ProdConsume> page = new Page<>(query.getStartPage(), query.getPageSize());
        // 执行分页查询
        Page<ProdConsume> prodPlansPageResult = baseMapper.selectProdConsumeWithTugId(page, query);
        // 构建分页响应 DTO
        PageResponseDto<ProdConsumeVO> pageResponseDto = new PageResponseDto(prodPlansPageResult, ProdConsumeVO.class);
        return pageResponseDto;
    }

    @Override
    public ProdConsumeVO detail(Long id) {
        return baseMapper.detail(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAndUpdate(ProdConsumeSaveReqDto dto) {
        ProdConsume prodConsume = MapperingUtils.copyProperties(dto, ProdConsume.class);
        prodConsume.setUpdateTime(new Date());
//        prodConsume.setUpdateBy(Long.parseLong(AuthUtil.getUserId()));
        prodConsume.setBeginDte(dto.getBeginDte());
        prodConsume.setEndDte(dto.getEndDte());
        ProdConsumeQueryDto query = new ProdConsumeQueryDto();
        query.setId(prodConsume.getId());
        query.setTugId(prodConsume.getTugId());
        query.setBeginDte(DateUtils.date2Str(dto.getBeginDte()));
        query.setEndDte(DateUtils.date2Str(dto.getEndDte()));
        query.setTugId(dto.getTugId());
        if (baseMapper.checkDate(query).size() != 0) {
            throw new BusinessRuntimeException(BizCode.BIZ_ERROR, "该日期内该拖轮存在数据油耗记录");
        }
        if (dto.getId() == null) {
//            prodConsume.setCreateBy(Long.parseLong(AuthUtil.getUserId()));
//            prodConsume.setCreateByName(AuthUtil.getUserName());
            prodConsume.setCreateTime(new Date());
            prodConsume.setCreateBy(DefaultCreateByConstant.userId);
            prodConsume.setCreateByName(DefaultCreateByConstant.userId.toString());
            save(prodConsume);
        } else {
            updateById(prodConsume);
        }

        //保存完后，更新作业计划的油耗记录
        //TODO 保存完后，更新作业计划的油耗记录
    }

    @Override
    public List<ProdConsumeVO> export(ProdConsumeQueryDto query) {
        long currentPage = 1;
        List<ProdConsumeVO> allRecords = Lists.newArrayList();

        while (true) {
            query.setStartPage(currentPage);
            PageResponseDto<ProdConsumeVO> page = this.treeList(query);
            List<ProdConsumeVO> records = page.getRecords();
            allRecords.addAll(records);

            if (records.size() < query.getPageSize()) {
                // 当前页的记录数小于 pageSize，说明已经是最后一页
                break;
            }
            currentPage++;
        }

        return allRecords;
    }
}
